<?php

if (isset($_GET["dbg"]) && $_GET["dbg"] == 'on') $debug = true;
else $debug = false; //error_reporting(0);

if ($debug) header ("Content-type: text/html");

if (!isset($_GET["x1"]) || !isset($_GET["x2"]) || !isset($_GET["x3"]) || !isset($_GET["x4"]))
  die("Error: No coordinates are provided!");
if ($_GET["x1"] == $_GET["x2"] || $_GET["x3"] == $_GET["x4"])
  die("Error: No graph can be created for this data!");

if (!isset($_GET["width"]))
  $_GET["width"] = 400;

$mval = array("width" => $_GET["width"], "height" => $_GET["width"]/2);

//initializations
$font = intval($mval["width"]/30);
$im = imagecreate ($mval["width"], $mval["height"]);
$background = imagecolorallocate($im, 255, 255, 220);
$text_color = imagecolorallocate ($im, 0, 0, 150);
$axle_color = imagecolorallocate ($im, 0, 0, 0);
$graph_color = imagecolorallocate ($im, 255, 0, 0);
$grid_color = imagecolorallocate ($im, 125, 125, 125);
$fill_color = imagecolorallocate ($im, 220, 255, 220);
$d = $mval["width"]/80;
$Y1 = $mval["height"]/4;
$X1 = $mval["width"]/6;
$textY = "m(x)";
$textX = "x";
$ifont = "timesi.ttf";
$sfont = "times.ttf";

//labels parameters
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $ifont, $textY);
$X_Left   = $lab_size[4] - $lab_size[0] + 3*$d;
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $ifont, "1");
$Y_Bottom = $lab_size[1] - $lab_size[5] + 3*$d;

//1
imageline($im, $X_Left - $d, $Y1, $X_Left + $d, $mval["height"]/4, $axle_color);
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $sfont, "1");
$x = $lab_size[4] - $lab_size[0];
$y = $lab_size[1] - $lab_size[5];
imagettftext($im, $font, 0, $X_Left - $x - 2*$d, $Y1 + $y/2, $text_color, $sfont, "1");

//Axle Y
imageline($im, $X_Left, $Y_Bottom, $X_Left, $mval["height"] - $Y_Bottom/2, $axle_color);
imageline($im, $X_Left, $Y_Bottom, $X_Left - $d, $Y_Bottom + 2*$d, $axle_color);
imageline($im, $X_Left, $Y_Bottom, $X_Left + $d, $Y_Bottom + 2*$d, $axle_color);
imagettftext($im, $font, 0, $d, $Y_Bottom, $text_color, $ifont, $textY);

//Axle X parameters
$X2 = ($_GET["x2"] - $_GET["x1"])/($_GET["x4"] - $_GET["x1"])*($mval["width"] - $X1 - $X1) + $X1;
$X3 = ($_GET["x3"] - $_GET["x1"])/($_GET["x4"] - $_GET["x1"])*($mval["width"] - $X1 - $X1) + $X1;

//fill
$values = array(
 0  => $X1,                          // x1
 1  => $mval["height"] - $Y_Bottom,  // y1
 2  => $X2,                          // x2
 3  => $Y1,                          // y2
 4  => $X3,                          // x3
 5  => $Y1,                          // y3
 6  => $mval["width"] - $X1,         // x4
 7  => $mval["height"] - $Y_Bottom,  // y4
);
imagefilledpolygon($im, $values, 4, $fill_color);

//Axle X
imageline($im, $X_Left/2, $mval["height"] - $Y_Bottom, $mval["width"] - $X_Left, $mval["height"] - $Y_Bottom, $axle_color);
imageline($im, $mval["width"] - $X_Left - 2*$d, $mval["height"] - $Y_Bottom - $d, $mval["width"] - $X_Left, $mval["height"] - $Y_Bottom, $axle_color);
imageline($im, $mval["width"] - $X_Left - 2*$d, $mval["height"] - $Y_Bottom + $d, $mval["width"] - $X_Left, $mval["height"] - $Y_Bottom, $axle_color);
imagettftext($im, $font, 0, $mval["width"] - $X_Left, $mval["height"] - $d, $text_color, $ifont, $textX);

//X4
imageline($im, $mval["width"] - $X1, $mval["height"] - $Y_Bottom - $d, $mval["width"] - $X1, $mval["height"] - $Y_Bottom + $d, $axle_color);
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $sfont, $_GET["x4"]);
$x = $lab_size[4] - $lab_size[0];
$y = $lab_size[1] - $lab_size[5];
imagettftext($im, $font, 0, $mval["width"] - $X1 - $x/2, $mval["height"] - $d, $text_color, $sfont, $_GET["x4"]);
//X1
imageline($im, $X1, $mval["height"] - $Y_Bottom - $d, $X1, $mval["height"] - $Y_Bottom + $d, $axle_color);
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $sfont, $_GET["x1"]);
$x = $lab_size[4] - $lab_size[0];
$y = $lab_size[1] - $lab_size[5];
imagettftext($im, $font, 0, $X1 - $x/2, $mval["height"] - $d, $text_color, $sfont, $_GET["x1"]);
//X2
imageline($im, $X2, $mval["height"] - $Y_Bottom - $d, $X2, $mval["height"] - $Y_Bottom + $d, $axle_color);
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $sfont, $_GET["x2"]);
$x = $lab_size[4] - $lab_size[0];
$y = $lab_size[1] - $lab_size[5];
imagettftext($im, $font, 0, $X2 - $x/2, $mval["height"] - $d, $text_color, $sfont, $_GET["x2"]);
//X3
imageline($im, $X3, $mval["height"] - $Y_Bottom - $d, $X3, $mval["height"] - $Y_Bottom + $d, $axle_color);
$lab_size = ImageTTFBBox ($font, 0/*angle*/, $sfont, $_GET["x3"]);
$x = $lab_size[4] - $lab_size[0];
$y = $lab_size[1] - $lab_size[5];
imagettftext($im, $font, 0, $X3 - $x/2, $mval["height"] - $d, $text_color, $sfont, $_GET["x3"]);

//gridlines
$style = array($grid_color, $grid_color, $grid_color, $grid_color, $grid_color, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT);
imagesetstyle($im, $style);
imageline($im, $X2, $mval["height"] - $Y_Bottom - $d, $X2, $Y1, IMG_COLOR_STYLED);
imageline($im, $X3, $mval["height"] - $Y_Bottom - $d, $X3, $Y1, IMG_COLOR_STYLED);
imageline($im, $X2, $Y1, $X_Left + $d, $Y1, IMG_COLOR_STYLED);

//Graph
imageline($im, $X1, $mval["height"] - $Y_Bottom, $X2, $Y1, $graph_color);
imageline($im, $X2, $Y1, $X3, $Y1, $graph_color);
imageline($im, $X3, $Y1, $mval["width"] - $X1, $mval["height"] - $Y_Bottom, $graph_color);

imagepng ($im);
imagedestroy ($im);

?>